package com.lineage.server.clientpackets;

import com.lineage.echo.ClientExecutor;
import com.lineage.server.model.Instance.L1PcInstance;
import com.lineage.server.model.Instance.L1PetInstance;
import com.lineage.server.model.L1Character;
import com.lineage.server.world.World;
import com.lineage.server.world.WorldPet;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

public class C_SelectTarget extends ClientBasePacket {
  private static final Log _log = LogFactory.getLog(C_SelectTarget.class);
  
  public void start(byte[] decrypt, ClientExecutor client) {
    try {
      read(decrypt);
      int petId = readD();
      int type = readC();
      int targetId = readD();
      L1PetInstance pet = WorldPet.get().get(Integer.valueOf(petId));
      if (pet == null)
        return; 
      L1Character target = (L1Character)World.get().findObject(targetId);
      if (target == null)
        return; 
      boolean isCheck = false;
      if (target instanceof L1PcInstance) {
        L1PcInstance tgpc = (L1PcInstance)target;
        if (tgpc.checkNonPvP(tgpc, (L1Character)pet))
          return; 
        isCheck = true;
      } else if (target instanceof com.lineage.server.model.Instance.L1DeInstance) {
        isCheck = true;
      } else if (target instanceof L1PetInstance) {
        isCheck = true;
      } else if (target instanceof com.lineage.server.model.Instance.L1SummonInstance) {
        isCheck = true;
      } else {
        if (target instanceof com.lineage.server.model.Instance.L1CnInstance)
          return; 
        if (target instanceof com.lineage.server.model.Instance.L1GamblingInstance)
          return; 
        if (target instanceof com.lineage.server.model.Instance.L1GamInstance)
          return; 
        if (target instanceof com.lineage.server.model.Instance.L1IllusoryInstance)
          return; 
        if (target instanceof com.lineage.server.model.Instance.L1SkinInstance) {
          over();
          return;
        } 
      } 
      if (isCheck && target.isSafetyZone())
        return; 
      pet.setMasterSelectTarget(target);
    } catch (Exception exception) {
    
    } finally {
      over();
    } 
  }
  
  public String getType() {
    return getClass().getSimpleName();
  }
}
